package com.eagle.roomsearch.controller.webSocketController.client;

import com.eagle.roomsearch.common.Result;
import com.eagle.roomsearch.controller.webSocketController.Message;
import com.eagle.roomsearch.controller.webSocketController.WebSocketHashTable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 林良炫
 * @Description: ${todo}
 * @date 2018/3/29  16:50
 */
@RestController
@RequestMapping(value = "/sendMessage")
public class SendMessageController {

    // todo http://localhost:8081/mvc?xage=1
    @GetMapping(value = "/mvc")
    @ResponseBody
    public String baseType(@RequestParam(value = "xage", required = false) int type) {
        return "type " + type;
    }


    // todo http://localhost:8081/sendMessage/userNumber?userNumber=3b175727529c4c8780562965d5a4a4a8,*****
    @GetMapping(value = "/userNumber")
    public Result send(@RequestParam(value = "userNumber") String userNumber) {

        String[] splitUserNumber = userNumber.split(",");
        List<Message> data = new ArrayList<>();
        for (String user : splitUserNumber) {
            Message message = setMessage(user);
            data.add(message);
        }
        return new Result(200, "消息發送成功", data);
    }

    private Message setMessage(@RequestParam(value = "userNumber") String userNumber) {
        //消息推送到websocket
        Message message = new Message();
        //创建时间
        LocalDateTime createTime = LocalDateTime.now();
        message.setTime(createTime);
        message.setMessage("親愛的 " + userNumber + "  ,你好" + "是來自服務器的問候");
        System.out.println("userNumber====" + userNumber);
        System.out.println("消息 = " + message);
        System.out.println("消息发送至 [" + userNumber + "] 状态：" + WebSocketHashTable.sendMessage(userNumber, message));
        return message;
    }

}
